package leetcode.datastructure.matrix;

/**
 * 此方法大数据量超时
 */
public class Program36ValidSudoku {
    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] columns = new int[9][9];
        int[][][] subboxs = new int[3][3][9];

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board.length; j++) {
                if (board[i][j] == '.') {
                    continue;
                }
                char x = board[i][j];
                int value = x - '0';
                if (rows[i][value-1] == 1 || columns[j][value-1] == 1 || subboxs[i / 3][j / 3][value-1] == 1) {
                    return false;
                }
                rows[i][value-1]++;
                columns[j][value-1]++;
                subboxs[i / 3][j / 3][value-1]++;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Program36ValidSudoku myStack = new Program36ValidSudoku();
        char[][] board = {
                {'5','3','.','.','7','.','.','.','.'}
                ,{'6','.','.','1','9','5','.','.','.'}
                ,{'.','9','8','.','.','.','.','6','.'}
                ,{'8','.','.','.','6','.','.','.','3'}
                ,{'4','.','.','8','.','3','.','.','1'}
                ,{'7','.','.','.','2','.','.','.','6'}
                ,{'.','6','.','.','.','.','2','8','.'}
                ,{'.','.','.','4','1','9','.','.','5'}
                ,{'.','.','.','.','8','.','.','7','9'}
        };
        System.out.println(myStack.isValidSudoku(board)); // 返回
    }
}
